<?php defined('SYSPATH') or die('No direct script access.');
require_once(APPPATH. 'classes/controller/My_Controller.php');
require_once(APPPATH. 'classes/model/Model_UserProfile.php');

class Controller_User extends My_Controller {

	public function action_index($uid = null) {
		$this->action_viewprofile($uid);
	}
	public function action_viewprofile($uid = null) {
		
		if($uid == null) $uid = $this->uid;
		else $uid = (int) My_URL::removeFakeExt($uid);
		
		$profile_model = new Model_UserProfile();
		$data = $profile_model->getUserProfileData($uid);
		
		$view = View::factory('editprofile');
		$view->data = $data;
		$view->view_only = true;
		if($uid == $this->uid) $view->access = Model_UserRelations::MYSELF;
		else {
			$ur = new Model_UserRelations();
			$view->access = $ur->getStatus($this->uid, $uid);
			$view->invite_url = "user/addfriend/".$uid;
		}
		$this->renderLoggedInUserView($view);
	}
	public function action_addfriend($uid) {
		$friend_uid = (int) My_URL::removeFakeExt($uid);
		$ul = new Model_UserLogin();
		$email = $ul->getEmail($friend_uid);
		$ur = new Model_UserRelations();
		$ur->sendFriendRequest($this->uid, $friend_uid);
		$link = My_URL::app_url('user/accept_friend/'.$this->uid);
		$message = " {$this->uid} sent you friend request (msg sent to $email).
			<a href='$link' target=_blank>click here</a> to confirm. ";
		$headers  = 'MIME-Version: 1.0' . "\r\n".
					'Content-type: text/html; charset=iso-8859-1' . "\r\n".
					'From: Couple Book <confirmation@couple-book.com>' . "\r\n" .
    				"To:  <$email>" . "\r\n";
		if(Kohana::$environment === Kohana::PRODUCTION) {
			mail($email, 'Welcome to couple book', $message, $headers, '-fconfirmation@couple-book.com');
			echo "Friend requesr sent.";
		} else {
			echo $message;
		}
	}
	public function action_invite_friend() {
		$email = Arr::get($_POST, 'email', '');
		if($email == '') {
			echo "invalid email";
			return;
		}
		$ul = new Model_UserLogin();
		$sent_uid = $ul->getUidByEmail($email, Model_UserLogin::NETWORK_SELF);
		if(!$sent_uid) {
			echo "given user does not exists.";
			return;
		}
		$ur = new Model_UserRelations();
		$ur->sendFriendRequest($this->uid, $sent_uid);
		$link = My_URL::app_url('user/accept_friend/'.$this->uid);
		$my_uid = $this->uid;
		$message = " $my_uid sent you couple request (msg sent to $email).
			<a href='$link' target=_blank>click here</a> to confirm. ";
		$headers  = 'MIME-Version: 1.0' . "\r\n".
					'Content-type: text/html; charset=iso-8859-1' . "\r\n".
					'From: Couple Book <confirmation@couple-book.com>' . "\r\n" .
    				"To:  <$email>" . "\r\n";
		if(Kohana::$environment === Kohana::PRODUCTION) {
			mail($email, 'Welcome to couple book', $message, $headers, '-fconfirmation@couple-book.com');
			echo "Friend requesr sent.";
		} else {
			echo $message;
		}
	}
	public function action_invite_couple() {
		if($this->partner_uid != null) {
			echo "already have partner";
			return;
		}
		$email = Arr::get($_POST, 'email', '');
		if($email == '') {
			echo "invalid email";
			return;
		}
		$ul = new Model_UserLogin();
		$sent_uid = $ul->getUidByEmail($email, Model_UserLogin::NETWORK_SELF);
		if(!$sent_uid) {
			echo "given user does not exists.";
			return;
		}
		$uf = new Model_UserProfile();
		$sent_sex = $uf->getSex($sent_uid);
		if($this->sex == $sent_sex) {
			echo "Same sex partner can not be couple.";
			exit();
		}
		$ur = new Model_UserRelations();
		$ur->sendCoupleRequest($this->uid, $sent_uid);
		$link = My_URL::app_url('user/accept_couple/'.Encrypt::instance()->encode($this->uid.'**'.$sent_uid));
		$my_uid = $this->uid;
		$message = " $my_uid sent you couple request (msg sent to $email).
			<a href='$link' target=_blank>click here</a> to confirm. ";
		$headers  = 'MIME-Version: 1.0' . "\r\n".
					'Content-type: text/html; charset=iso-8859-1' . "\r\n".
					'From: Couple Book <confirmation@couple-book.com>' . "\r\n" .
    				"To:  <$email>" . "\r\n";
		if(Kohana::$environment === Kohana::PRODUCTION) {
			mail($email, 'Welcome to couple book', $message, $headers, '-fconfirmation@couple-book.com');
			echo "Friend requesr sent.";
		} else {
			echo $message;
		}
	}
	public function action_accept_friend($uid) {
		$uid = My_URL::removeFakeExt($uid);
		$ur = new Model_UserRelations();
		if($ur->acceptFriendRequest($uid, $this->uid) < 1) {
			echo "Not a valid friend request";
		} else {
			echo "Now you are friend with this person.";
		}
		//echo Request::factory('')->execute();
	}
	public function action_accept_couple($data) {
		if($this->partner_uid != null) {
			echo "already have partner";
			return;
		}
		$data = My_URL::removeFakeExt($data);
		$decoded = Encrypt::instance()->decode($data);
		if($decoded == false) {
			echo "Invalid request";
			return;
		}
		$arr = explode('**', $decoded);
		if(count($arr) != 2 || $arr[1] != $this->uid) {
			echo "Invalid request";
			return;
		}
		$ur = new Model_UserRelations();
		if($ur->acceptCoupleRequest($arr[0], $this->uid)< 1) {
			echo "Not a valid couple request";
		} else {
			echo "Now you are couple with this person. Enjoy surfing this site.";
		}
	}
	public function action_editprofile() {
		$profile_model = new Model_UserProfile();
		$data = $profile_model->getUserProfileData($this->uid);
		
		$view = View::factory('editprofile');
		$view->data = $data;
		$this->renderLoggedInUserView($view);
	}
	public function action_search() {
		$type = Arr::get($_POST, 'type', '');
		$keyword = Arr::get($_POST, 'keyword', '');
		if($type == '' || $keyword == '') return;
		if($type == 'email') {
			$ul = new Model_UserLogin();
			$results = $ul->getUsers($keyword);
			if(count($results) < 1) echo "no results";
			else foreach ($results as $user) {
				echo "<a href='".My_URL::app_url('user/viewprofile/'.$user['uid'])."'>$user[username]</a> ";
			}
		}
	}
	public function action_edit() {
		$data = array();
		$data['f_name'] = Arr::get($_POST, 'f_name', '');
		if($data['f_name'] == '') unset($data['f_name']);
		
		$data['l_name'] = Arr::get($_POST, 'l_name', '');
		if($data['l_name'] == '') unset($data['l_name']);
		
		$data['about_me'] = Arr::get($_POST, 'about_me', '');
		if($data['about_me'] =='') unset($data['about_me']);
		
		$data['religion'] = Arr::get($_POST, 'religion');
		if($data['religion']=='') unset($data['religion']);
		
		$data['address'] = Arr::get($_POST, 'address');
		if($data['address'] == '') unset($data['address']);
		
		$data['city'] = Arr::get($_POST, 'city');
		if($data['city'] == '') unset($data['city']);
		
		$data['zip'] = Arr::get($_POST, 'zip');
		if($data['zip'] == '') unset($data['zip']);
		
		$data['country'] = Arr::get($_POST, 'country');
		if($data['country'] == '') unset($data['country']);
		
		$data['phone'] = Arr::get($_POST, 'phone');
		if($data['phone'] == '') unset($data['phone']);
		
		$data['movies'] = Arr::get($_POST, 'movies');
		if($data['movies'] == '') unset($data['movies']);
		
		$data['songs'] = Arr::get($_POST, 'songs');
		if($data['songs'] == '') unset($data['songs']);
		
		$data['holiday_place'] = Arr::get($_POST, 'holiday_place');
		if($data['holiday_place'] == '') unset($data['holiday_place']);
		
		$data['interests'] = Arr::get($_POST, 'interests');
		if($data['interests'] == '') unset($data['interests']);
		
		$data['meet_how'] = Arr::get($_POST, 'meet_how');
		if($data['meet_how'] == '') unset($data['meet_how']);
		
		$data['meet_time'] = Arr::get($_POST, 'meet_time');
		if($data['meet_time'] == '') unset($data['meet_time']);
		
		$data['meet_desc'] = Arr::get($_POST, 'meet_desc');
		if($data['meet_desc'] == '') unset($data['meet_desc']);
		
		$data['propose'] = Arr::get($_POST, 'propose');
		if($data['propose'] == '') unset($data['propose']);

		
		$data['lang'] = Arr::get($_POST, 'lang');
		if($data['lang'] == '') unset($data['lang']);
		
		$data['sports'] = Arr::get($_POST, 'sports');
		if($data['sports'] == '') unset($data['sports']);
		
		$data['books'] = Arr::get($_POST, 'books');
		if($data['books'] == '') unset($data['books']);
		
		$data['tv_shows'] = Arr::get($_POST, 'tv_shows');
		if($data['tv_shows'] == '') unset($data['tv_shows']);
		
		$data['cuisines'] = Arr::get($_POST, 'cuisines');
		if($data['cuisines'] == '') unset($data['cuisines']);

		$data['hschool'] = Arr::get($_POST, 'hschool');
		if($data['hschool'] == '') unset($data['hschool']);
		
		$data['clg'] = Arr::get($_POST, 'clg');
		if($data['clg'] == '') unset($data['clg']);
		
		$data['occupation'] = Arr::get($_POST, 'occupation');
		if($data['occupation'] == '') unset($data['occupation']);
		
		$data['rel_think'] = Arr::get($_POST, 'rel_think');
		if($data['rel_think'] == '') unset($data['rel_think']);
		
		$data['wemail'] = Arr::get($_POST, 'wemail');
		if($data['wemail'] == '') unset($data['wemail']);
		//upload image file
		//print_r($_FILES); return;
		$files = new Validation($_FILES);
		$files->rule('pic', 'Upload::not_empty');
		$files->rule('pic', 'Upload::valid');
		$files->rule('pic', 'Upload::type', array(':value', array('jpg', 'png', 'gif')));
		$files->rule('pic', 'Upload::size', array(':value', '2.5MiB'));
		if(isset($_FILES['pic']) && $files->check()) {
			Upload::$default_directory = DOCROOT.'profile_pics';
			Upload::$remove_spaces = false;
			$filename = "pic_".$this->uid.".".strtolower(pathinfo($_FILES['pic']['name'], PATHINFO_EXTENSION));
			Upload::save($_FILES['pic'], $filename);
			$data['pic_url'] = My_URL::app_url("profile_pics/".$filename, false);
		}
		$profile_model = new Model_UserProfile();
		if(!empty($data)) {
			$profile_model->updateUserProfile($this->uid, $data);
		}
		Request::factory('user/viewprofile')->execute();
	}
}